/**
 * Created by .
 * User: Rekey
 * Date: 11-5-13
 * Time: 下午9:28
 * To change this template use File | Settings | File Templates.
 */
(function ($) {
  function getScript(js) {
    var head = $('head')[0];
    var s = document.createElement('script');
    s.onload = s.onreadystatechange = function () {
      if (!s.readyState || s.readyState == 'complete' || s.readyState == 'loaded') {
        s.onreadystatechange = null;
        head.removeChild(s);
      }
    };
    s.src = js;
    head.appendChild(s);
  }

  $('.audio-player').each(function () {
    var item = $(this);
    var url = '/xiami/song/' + item.data('id');
    var callbackName = 'jQuery_xiami_' + item.data('id');
    window[callbackName] = function (e) {
      if (e.url) {
        var id = 'id' + (+new Date);
        item.find('audio').attr('src', e.url);
        item.find('span').remove();
      }
      window[callbackName] = null;
    };
    getScript(url + '?callback=' + callbackName);
  });
})(jQuery);
